package Classes.GameDatas// ActionScript file
{
	import Classes.GameUtils.GU_DataTransform;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_Zombie;
	import Classes.Interfaces.GV_hot;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.Language;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;

	/**
	 *僵尸数据类 
	 * @author 葛增文
	 * 
	 */	
	public class GD_Zombie extends GameData implements GV_hot
	{
		private var _map:GD_Map;/*所在地图*/
		private var _serverID:Number;/*服务器ID*/
		private var _zombieType:Number;/*僵尸配置类型*/
		private var _life:int;/*当前生命*/
		private var _isDead:Boolean=false;/*是否已死亡*/
		private var _view:MN_Zombie;/*僵尸显示对象*/
		private var _zombieCoordinate:Number;/*僵尸位置，为-1则随机生成到地图上面，其他的则为指*/
		
		/**
		 * 僵尸静态表对象 
		 */		
		private static var  _zombieObj:Object;
		
		/**
		 * 当前的僵尸对象 
		 */		
		private var _currentZombieObj:Object;
		
		
		/**
		 *僵尸属性不正常的个数 
		 */
		private  var _sxTxtCount:uint=0;
 
		public function GD_Zombie(zombieType:Number):void
		{
			this._zombieType=zombieType;
			this.init();
		}
		
		private function init():void
		{
			this._currentZombieObj=zombieObj[this._zombieType];
		}
		
		/**
		 *创建一个僵尸数据对象
		 * @param creatObj 创建数据对象
		 * @return 僵尸数据对象
		 * 
		 */		
		public static function creat(creatObj:Object,map:GD_Map):GD_Zombie
		{
			var returnZombie:GD_Zombie=new GD_Zombie(creatObj.zombieType);
			returnZombie._map=map;
			returnZombie._serverID=creatObj.zombieID;
			returnZombie.life=creatObj.life;
			returnZombie.zombieCoordinate=creatObj.coordinate;
			return returnZombie;
		}
		
		/**
		 * 僵尸基本信息对象 
		 */
		public static function get zombieObj():Object
		{
			if(null==_zombieObj)
			{
				_zombieObj=new Object();
				var configStr:String=M_ConfigData.instance.getCSVByName("僵尸配置表");
				var configArr:Array=GU_DataTransform.csvTransformObjsAry(configStr,19);
				for each (var obj:Object in configArr)
				{
					_zombieObj[obj.zt_zombie_id]=obj;
				}
			}
			return _zombieObj;
		}
		
		/**
		 *检测攻击后果 
		 * @param attType 攻击类型
		 * @return 
		 * 0：攻击无效
		 * 1：普通攻击
		 * 2：弱点攻击
		 * 
		 */		
		public function checkAttackResult(attType:int):int
		{
			return 1;
		}
		
		/**
		 *僵尸血量变化 
		 * @param changedValue
		 * 
		 */		
		public function lifeChanged(changedValue:int):void
		{
			if(changedValue<this.life)GU_Sound.instance.playCommSound("Zombie_BeHitted");
			this.life=changedValue;
			if(changedValue<=this.lifeMax/2){
				if( !(this.view.txStMap.hasOwnProperty(10004008) ) || this.view.txStMap[10004008]==null)
				this.view.setState(10004008,true);
			}
			
			if(this.life<1)this.die();
		}
		
		/**
		 *僵尸死亡 
		 * 
		 */		
		private function die():void
		{
			this._isDead=true;
			this.view.actDie();
			this.view.releaseStates();
			//this.release();
		}
		
		override public function release():void
		{
			var bl:Boolean	= this.zombieType.toString().slice(0,3)=="211";
			
			
			super.release();
			this.map.removeZombieByID(this.serverID);
			if(this._view)
			{
				//this._view.removeSign=1;
				this._view.release();
				this._view=null;
			}
//			_zombieObj=null;//********
			
			if(bl)GD_HavenWall.inbreakCheck();//入侵检查  请在释放后再检查
		}

		/*getter & setter*/
			
		public function get zombieName():String
		{
			return this._currentZombieObj.zt_zombie_name;
		}
		
		public function get zombieTypeString():String
		{
			return String(_zombieType);
		}
		
		public function get zombieKind():String
		{
			return this._currentZombieObj.zt_zombie_type;
		}
		
		/**
		 *移动速度 
		 * @return 
		 * 
		 */
		public function get speed():Number
		{
//			return 40;
			return this._currentZombieObj.zt_zombie_movespeed;
			//return this.configXML.speed;
		}
		
		public function get lifeMax():int
		{
			return this._currentZombieObj.zt_zombie_blood;
		}
		
		/**
		 *僵尸皮肤类型 
		 * @return 
		 * 
		 */		
		public function get zombieSkinType():String
		{
			return String(this._currentZombieObj.zombie_skins);
		}
		
		/**
		 * 攻击力 
		 * @return 
		 */		
		public function get att():int
		{
//			return this._currentZombieObj.zt_zombie_power;
			return this._currentZombieObj.zt_zombie_attack;
		}
		
		/**
		 *攻击范围 
		 * @return 
		 */
		public function get attRange():int
		{
//			return 3;
			return this._currentZombieObj.zt_zombie_range;
//			return this.configXML.attRange;
		}
		
		/**
		 * 搜索范围
		 * @return 
		 * 
		 */		
		public function get findRange():int
		{
//			return 10;
			return this._currentZombieObj.zt_zombie_sight;
			//return this.configXML.findRange;
		}
		/**
		 * 追击返回
		 * @return 
		 * 
		 */		
		public function get chaseRange():int
		{
//			return 20;
			return this._currentZombieObj.zt_zombie_chase;
			//return this.configXML.chaseRange;
		}

		public function get serverID():Number
		{
			return _serverID;
		}

		public function get life():int
		{
			return _life;
		}

		public function set life(value:int):void
		{
			_life = value;
			this.touchObserve("life");
		}

		public function get isDead():Boolean
		{
			return _isDead;
		}

		public function set isDead(value:Boolean):void
		{
			_isDead = value;
		}

		public function get view():MN_Zombie
		{
			if(!this._view)this._view=new MN_Zombie(this);
			
			if(_currentZombieObj.zt_zombie_power!="no"){
				_view.setStates(_currentZombieObj.zt_zombie_power,true);
//				_view.setStates(_currentZombieObj.zt_zombie_power,false);
//				var timer:Timer= new Timer(2000,1);
//				timer.addEventListener(TimerEvent.TIMER,function(e:Event):void{
//					_view.setStates(_currentZombieObj.zt_zombie_power,true);
//				});
//			timer.start();
			}
			return _view;
		}

		public function get map():GD_Map
		{
			return _map;
		}

		public function get zombieType():Number
		{
			return _zombieType;
		}

		/**
		 * 当前的僵尸对象 
		 */	
		public function get currentZombieObj():Object
		{
			return _currentZombieObj;
		}

		//****************热感的属性******************
		public function get currentlife():int
		{
			return this.life;
		}
		public function get gvName():String
		{
			return this.zombieName
		}
		
		public function get sxTxt():String			//僵尸属性热感文本
		{
			var typeObj:Object={1:Language.ztSxTxtZC,2:Language.ztSxTxtKX,3:Language.ztSxTxtRD};
			
			_sxTxtCount	=	0;
			var allTxt:String	="";//"侵蚀:@QZ,闪电:@SD\n火焰:@HY,物理:@WL";
			var sxTxt:String	="";
			
			var objary:Array	=	new Array;
			
			
			if(this.currentZombieObj.zt_111_type != 1){
				allTxt+= _sxTxtCount%2!=0?",": _sxTxtCount>0?"\n":"";
				allTxt+= addcolor(this.currentZombieObj.zt_111_type,"侵蚀:")	+	typeObj[this.currentZombieObj.zt_111_type];
				_sxTxtCount++;
				objary.push( {type:"侵蚀",value:this.currentZombieObj.zt_111_type,str:typeObj[this.currentZombieObj.zt_111_type] + addcolor(this.currentZombieObj.zt_111_type,":侵蚀")});
			}
			
			if(this.currentZombieObj.zt_112_type != 1){
				allTxt+= _sxTxtCount%2!=0?",": _sxTxtCount>0?"\n":"";
				allTxt+= addcolor(this.currentZombieObj.zt_112_type,"闪电:")	+	typeObj[this.currentZombieObj.zt_112_type];
				_sxTxtCount++;
				objary.push( {type:"闪电",value:this.currentZombieObj.zt_112_type,str:typeObj[this.currentZombieObj.zt_112_type] + addcolor(this.currentZombieObj.zt_112_type,":闪电")});
			}
			
			if(this.currentZombieObj.zt_113_type != 1){
				allTxt+= _sxTxtCount%2!=0?",": _sxTxtCount>0?"\n":"";
				allTxt+= addcolor(this.currentZombieObj.zt_113_type,"火焰:")	+	typeObj[this.currentZombieObj.zt_113_type];
				_sxTxtCount++;
				objary.push( {type:"火焰",value:this.currentZombieObj.zt_113_type,str:typeObj[this.currentZombieObj.zt_113_type] + addcolor(this.currentZombieObj.zt_113_type,":火焰")});
			}
			
			if(this.currentZombieObj.zt_114_type != 1){
				allTxt+= _sxTxtCount%2!=0?",": _sxTxtCount>0?"\n":"";
				allTxt+= addcolor(this.currentZombieObj.zt_114_type,"物理:")	+	typeObj[this.currentZombieObj.zt_114_type];
				_sxTxtCount++;
				objary.push( {type:"物理",value:this.currentZombieObj.zt_114_type,str:typeObj[this.currentZombieObj.zt_114_type] + addcolor(this.currentZombieObj.zt_114_type,":物理")});
			}
				
			sxTxt				=	Language.sxTxt.replace("@ALLTXT",allTxt);	
			
		
			//--------------------------------------------------------    抗性的放在左边
			var aryRD:Array	=	new Array;		//弱点数组	
			var aryKX:Array	=	new Array;		//抗性数组
			var i:uint=0;
			for( ; i<objary.length; i++)
			{
				if( objary[i].value == 2)aryKX.push(objary[i]);		//2为抗性
				else
					aryRD.push( objary[i]);
			}
			
			var resultary:Array	=	new Array;
			var sxTxt1:String	=	"";
			
			if(_sxTxtCount < 1)return "";

			if(aryKX.length >0)resultary.push( aryKX.shift());
			else
				resultary.push( aryRD.shift());
			
			i=0;
			while(aryKX.length || aryRD.length){
				if( resultary[i].value == 2){						//如果前一个是抗性就加弱点进来
					if( aryRD.length>0)resultary.push( aryRD.shift())
					else
						resultary.push( aryKX.shift());
				}
				else{
					if( aryKX.length>0)resultary.push( aryKX.shift())
					else
						resultary.push( aryRD.shift());
				}
				
			}
			
			for(i=0; i<resultary.length; i++)
			{
				sxTxt1	+=	resultary[i].str;
				sxTxt1  +=	(i+1)%2==0?"\n":( (i+1)==resultary.length?"":",");
			}
			//----------------------------------------------------------- 抗性的放在左边
			
			return Language.sxTxt.replace("@ALLTXT",sxTxt1);
//			trace(resultary.length, resultary.toString(), sxTxt1);
			
//			trace(sxTxt);
//			return sxTxt;
		}
		
		private function addcolor(sxType:uint, str:String):String
		{
			var rstr:String="";
			switch(sxType)
			{
				case 1:rstr="<font color='#e0b44b'>"+str+"</font>";break;
				case 2:rstr="<font color='#FF0000'>"+str+"</font>";break;
				case 3:rstr="<font color='#00FF00'>"+str+"</font>";break;
			}
			return rstr;
		}
		
		public function get sxTxtCount():uint
		{
			return this._sxTxtCount;
		}
		
		public function addObserverFuncs(observerType:String,obderverObj:*):void
		{
			this.addObserver(observerType,obderverObj);
		}
		
		public function removeOberverFuns(observerType:String,obderverObj:*):void
		{
			this.removeObserver(observerType,obderverObj);
		}

		/**
		 * 僵尸位置，为-1则随机生成到地图上面，其他的则为指 
		 * @return 
		 */		
		public function get zombieCoordinate():Number
		{
			return _zombieCoordinate;
		}

		public function set zombieCoordinate(value:Number):void
		{
			_zombieCoordinate = value;
		}
		
		public function get positionPoint():Point
		{
			var atx:int=this.zombieCoordinate % this.map.widthNum;
			var aty:int=int(this.zombieCoordinate / this.map.widthNum);
			return new Point(atx, aty);
		}

		
		//**********************************

	}
}